package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CaseServiceImpl implements CaseService {
    @Autowired
    private ToolsResposity toolsResposity;

    private SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Map addCase(String name, boolean is_public) {
        String create_time = sdf_.format(new Date());
        Map returnMap = new HashMap();
        String sql = "SELECT * FROM public.t_case where name = '" + name + "'";
        Map caseMap = toolsResposity.exeSql(sql);
        if (caseMap == null) {
            String sql_insert = "INSERT INTO public.t_case(name,is_public,create_time) VALUES ('" + name + "'," + is_public + ",'" + create_time + "');";
            toolsResposity.exeSql(sql_insert);
            returnMap.put("success", true);
        } else {
            returnMap.put("success", false);
            returnMap.put("msg", "表单项目已经存在");
        }

        return returnMap;
    }

    @Override
    public Map editCase(String id, String name, String caseformids, String casebaselayerids, String casespeciallayerids, String caseeditlayerid) {
        String sql = "UPDATE t_case SET name = '" + name + "' WHERE id ='" + id + "'";
        toolsResposity.exeSql(sql);

        //更新t_form_content表,先删除，再插入

        //删除
        String sql_delete = "delete from t_case_form where caseid =" + id;
        toolsResposity.exeSql(sql_delete);
        //更新
        if (!"".equals(caseformids)) {
            String caseformidArr[] = caseformids.split(",");
            for (int i = 0; i < caseformidArr.length; i++) {
                String caseformid = caseformidArr[i];
                String sql_caseform = "select * from t_form where id = " + caseformid;
                Map caseformtMap = toolsResposity.exeSql(sql_caseform);
                if (caseformtMap != null) {
                    String formid = caseformtMap.get("id").toString();
                    String formname = caseformtMap.get("name").toString();
                    String sql_insert = "INSERT INTO public.t_case_form(caseid,formid,casename,formname) VALUES (" + id + ",'" + formid + "','" + name + "','" + formname + "');";
                    toolsResposity.exeSql(sql_insert);
                }

            }
        }

        //删除
        String sql_case_baselayer_delete = "delete from t_case_baselayer where caseid =" + id;
        toolsResposity.exeSql(sql_case_baselayer_delete);
        //更新t_case_baselayer
        if (!"".equals(casebaselayerids)) {
            String caselayeridArr[] = casebaselayerids.split(",");
            for (int i = 0; i < caselayeridArr.length; i++) {
                String caselayerid = caselayeridArr[i];
                String sql_caselayer = "select * from t_layer where id = " + caselayerid;
                Map caselayerMap = toolsResposity.exeSql(sql_caselayer);
                if (caselayerMap != null) {
                    String layerid = caselayerMap.get("id").toString();
                    String layername = caselayerMap.get("name").toString();
                    String layercname = caselayerMap.get("cname").toString();
                    String type = caselayerMap.get("type").toString();
                    String url = "";
                    if (caselayerMap.get("url") != null) {
                        url = caselayerMap.get("url").toString();
                    }
                    String sql_insert = "INSERT INTO public.t_case_baselayer(caseid,layerid,casename,layername,url,type) VALUES (" + id + ",'" + layerid + "','" + name + "','" + layername + "','" + url + "','" + type + "');";
                    toolsResposity.exeSql(sql_insert);
                }

            }
        }
        //删除t_case_speciallayer
        String sql_case_speciallayer_delete = "delete from t_case_speciallayer where caseid =" + id;
        toolsResposity.exeSql(sql_case_speciallayer_delete);
        //更新
        if (!"".equals(casespeciallayerids)) {
            String caselayeridArr[] = casespeciallayerids.split(",");
            for (int i = 0; i < caselayeridArr.length; i++) {
                String caselayerid = caselayeridArr[i];
                String sql_caselayer = "select * from t_layer where id = " + caselayerid;
                Map caselayerMap = toolsResposity.exeSql(sql_caselayer);
                if (caselayerMap != null) {
                    String layerid = caselayerMap.get("id").toString();
                    String layername = caselayerMap.get("name").toString();
                    String layercname = caselayerMap.get("cname").toString();
                    String type = caselayerMap.get("type").toString();
                    String url = "";
                    if (caselayerMap.get("url") != null) {
                        url = caselayerMap.get("url").toString();
                    }
                    String sql_insert = "INSERT INTO public.t_case_speciallayer(caseid,layerid,casename,layername,url,type) VALUES (" + id + ",'" + layerid + "','" + name + "','" + layername + "','" + url + "','" + type + "');";
                    toolsResposity.exeSql(sql_insert);
                }

            }
        }

        //删除
        String sql_case_editlayer_delete = "delete from t_case_editlayer where caseid =" + id;
        toolsResposity.exeSql(sql_case_editlayer_delete);
        //更新
        if (!"".equals(caseeditlayerid)) {
            String sql_caseeditlayer = "select * from t_layer where id = " + caseeditlayerid;
            Map caseeditlayerMap = toolsResposity.exeSql(sql_caseeditlayer);
            String layerid = caseeditlayerMap.get("id").toString();
            String layername = caseeditlayerMap.get("name").toString();
            String layercname = caseeditlayerMap.get("cname").toString();
            String type = caseeditlayerMap.get("type").toString();
            String url = "";
            if (caseeditlayerMap.get("url") != null) {
                url = caseeditlayerMap.get("url").toString();
            }

            String sql_insert = "INSERT INTO public.t_case_editlayer(caseid,layerid,casename,layername,url,type) VALUES (" + id + ",'" + layerid + "','" + name + "','" + layername + "','" + url + "','" + type + "');";
            toolsResposity.exeSql(sql_insert);
        }
        return null;

    }

    @Override
    public List getAllCases() {
        String sql = "SELECT * FROM public.t_case";
        return toolsResposity.exeSqlList(sql);
    }

    /*@Override
    public Map delCaseById(String id) {
        Map returnMap = new HashMap();

        String sql_case_form = "select * from t_case_form where caseid in (" + id + ")";
        List caseContentMap = toolsResposity.exeSqlList(sql_case_form);
        String sql_case_layer = "select * from t_case_layer where caseid in (" + id + ")";
        List caseLayerMap = toolsResposity.exeSqlList(sql_case_layer);

        if (caseContentMap.size() > 0||caseLayerMap.size()>0) {
            //不能删
            returnMap.put("deleted", false);
            returnMap.put("msg", "该方案表已被使用，不能删除");
        } else {
            //可以删除,删除t_case
            String sql_delete = "delete from t_case where id in (" + id + ")";
            toolsResposity.exeSql(sql_delete);

            //删除t_case_layer
            String sql_case_layer_delete = "delete from t_case_layer where caseid in (" + id + ")";
            toolsResposity.exeSql(sql_case_layer_delete);

            //删除t_case_form
            String sql_case_form_delete = "delete from t_case_form where caseid in (" + id + ")";
            toolsResposity.exeSql(sql_case_form_delete);

            returnMap.put("deleted", true);
        }

        return returnMap;
    }*/

    @Override
    public Map delCaseById(String id) {
        Map returnMap = new HashMap();

        String sql_task = "select * from t_task where caseid in (" + id + ")";
        List taskMap = toolsResposity.exeSqlList(sql_task);


        if (taskMap.size() > 0) {
            //不能删
            returnMap.put("deleted", false);
            returnMap.put("msg", "该方案表已被使用，不能删除");
        } else {
            //可以删除,删除t_case
            String sql_delete = "delete from t_case where id in (" + id + ")";
            toolsResposity.exeSql(sql_delete);

            //删除t_case_baselayer
            String sql_case_baselayerlayer_delete = "delete from t_case_baselayer where caseid in (" + id + ")";
            toolsResposity.exeSql(sql_case_baselayerlayer_delete);

            //删除t_case_speciallayer
            String sql_case_speciallayer_delete = "delete from t_case_speciallayer where caseid in (" + id + ")";
            toolsResposity.exeSql(sql_case_speciallayer_delete);

            //删除t_case_editlayer
            String sql_case_editlayer_delete = "delete from t_case_editlayer where caseid in (" + id + ")";
            toolsResposity.exeSql(sql_case_editlayer_delete);

            //删除t_case_form
            String sql_case_form_delete = "delete from t_case_form where caseid in (" + id + ")";
            toolsResposity.exeSql(sql_case_form_delete);


            returnMap.put("deleted", true);
        }

        return returnMap;
    }

    @Override
    public Map getBindingByCaseId(String id) {

        String sql_case_form = "SELECT * FROM public.t_case_form where caseid = " + id;
        List caseFormList = toolsResposity.exeSqlList(sql_case_form);

        String sql_case_baselayer = "SELECT * FROM public.t_case_baselayer where caseid = " + id;
        List casebaseLayerList = toolsResposity.exeSqlList(sql_case_baselayer);

        String sql_case_speciallayer = "SELECT * FROM public.t_case_speciallayer where caseid = " + id;
        List casespecialLayerList = toolsResposity.exeSqlList(sql_case_speciallayer);

        String sql_case_editlayer = "SELECT * FROM public.t_case_editlayer where caseid = " + id;
        List caseEditLayerList = toolsResposity.exeSqlList(sql_case_editlayer);

        Map returnMap = new HashMap();
        returnMap.put("form", caseFormList);
        returnMap.put("baselayer", casebaseLayerList);
        returnMap.put("special", casespecialLayerList);
        returnMap.put("editlayer", caseEditLayerList);
        return returnMap;
    }

    /**
     * @return
     * @Author zxw
     * @Description //TODO 修改原来接口 未考虑坐标转换
     * @Date 2023/12/5  14:41
     **/
    @Override
    public Map getLayersByCaseId(String caseid) {
        String sql_case_baselayer = "SELECT * FROM public.t_case_baselayer where caseid = " + caseid;
        List casebaseLayerList = toolsResposity.exeSqlList(sql_case_baselayer);

        String sql_case_speciallayer = "SELECT * FROM public.t_case_speciallayer where caseid = " + caseid;
        List casespecialLayerList = toolsResposity.exeSqlList(sql_case_speciallayer);

        String sql_case_editlayer = "SELECT * FROM public.t_case_editlayer where caseid = " + caseid;
        Map caseEditLayerMap = toolsResposity.exeSql(sql_case_editlayer);
        String layername = caseEditLayerMap.get("layername").toString();

//        String sql = "SELECT ST_AsText(geom) FROM " + layername;
        String sql = "SELECT st_astext(ST_Transform(geom,4326)) FROM " + layername;
        List editListPolygon = toolsResposity.exeSqlList(sql);

        Map returnMap = new HashMap();
        returnMap.put("baselayer", casebaseLayerList);
        returnMap.put("special", casespecialLayerList);
        returnMap.put("editLayerPolygon", filterGeo(editListPolygon));
        return returnMap;
    }

    private List filterGeo(List list) {
        List returnList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            List listPolygon = new ArrayList();

            String st_astext = (String) map.get("st_astext");
            if (st_astext.contains("MULTIPOLYGON")) {
                st_astext = st_astext.substring(15, st_astext.length() - 3);

                String[] polygons = st_astext.split("\\)\\),\\(\\(");

                for (int j = 0; j < polygons.length; j++) {
                    String polygon = polygons[j];
                    if (polygon.contains("),(")) {
                        String[] polygonHole = polygon.split("\\),\\(");
                        listPolygon.add(polygonHole);
                    } else {
                        listPolygon.add(polygon);
                    }
                }

                map.put("geo", listPolygon);

            } else if (st_astext.contains("POLYGON")) {
                st_astext = st_astext.substring(9, st_astext.length() - 2);
                map.put("geo", st_astext);
            } else {
                st_astext = st_astext.substring(6, st_astext.length() - 2);
                map.put("geo", st_astext);
            }


            returnList.add(map);
        }
        return returnList;
    }

    @Override
    public List getCasesByParam(boolean is_public) {
        String sql = "SELECT * FROM public.t_case where is_public = " + is_public;
        return toolsResposity.exeSqlList(sql);
    }


    @Override
    public List<String> queryTableDeptUnDistribute() {
        return new ArrayList<>();

    }

    @Override
    public void delTableDept(String tablename) {

    }

    @Override
    public List queryTableDept() {
        return null;
    }

    @Override
    public void editTableDept(String tablename) {

    }
}

